<?php

namespace app\common\service\queue;

use addons\tools\model\UserVip as UserVipModel;
use addons\tools\model\UserVipRule as UserVipRuleModel;
use addons\tools\model\MealRule as MealRuleModel;

class User
{

    /**
     * 用户每天免费会员重置次数
     * @return void
     */
    public function update_user_day_handle()
    {
        $userVipRuleModel = new UserVipRuleModel();
        $mealRule = new MealRuleModel();
        $ruleList = $mealRule->field('id,number')->where('meal_id', 1)->select();
        if ($ruleList) {
            foreach ($ruleList as $v) {
                $userVipRuleModel->where('rule_id', $v['id'])->update(['value' => $v['number']]);
            }
        }
    }

    public function update_free_meal_handle()
    {
        $userVipModel = new UserVipModel();
        $userVip = $userVipModel->where('meal_id', 1)->find();
        if ($userVip) {
            $mealRule = new MealRuleModel();
            $userVipRuleModel = new UserVipRuleModel();
            $ruleList = $mealRule->field('id,number,tool_id')->where('meal_id', 1)->select();
            $data = [];
            foreach ($ruleList as $k => $v) {
                if (!$userVipRuleModel->where('rule_id', $v['id'])->value('id')) {
                    $data[$k]['user_id'] = $userVip->user_id;
                    $data[$k]['value'] = $v['number'];
                    $data[$k]['rule_id'] = $v['id'];
                    $data[$k]['status'] = 1;
                    $data[$k]['tool_id'] = $v['tool_id'];
                }
            }
            if(!empty($data))
                $userVipRuleModel->saveAll($data);
        }

    }

    public function update_user_handle()
    {

        $userVipModel = new UserVipModel();
        $user = $userVipModel->with(['meal' => function ($query) {
            $query->field('id,days');
        }])->where('update_vip', 1)->find();
        if ($user) {
            $userVipRuleModel = new UserVipRuleModel();
            $mealRuleModel = new MealRuleModel();
            $attach = json_decode($user->attach, true);
            $mealId = $user->meal_id;
            if ($attach && $attach['meal_id'])
                $mealId = $attach['meal_id'];
            $mealRuleList = $mealRuleModel->field('id,tool_id,number')->where('meal_id', $mealId)
                ->where('status', 'normal')->select();
            if ($mealRuleList) {
                //先删除之前
                $userRuleArr = $userVipRuleModel->where('user_id', $user->user_id)
                    ->column('value', 'tool_id');
                $data = [];
                foreach ($mealRuleList as $k => $v) {
                    $data[$k]['user_id'] = $user->user_id;
                    $data[$k]['rule_id'] = $v['id'];
                    $data[$k]['tool_id'] = $v['tool_id'];
                    if (!empty($userRuleArr[$v['tool_id']])) {
                        $data[$k]['value'] = $v['number'] + $userRuleArr[$v['tool_id']];
                    } else {
                        $data[$k]['value'] = $v['number'];
                    }
                }
                $userVipRuleModel->where('user_id', $user->user_id)->delete();

                $userVipRuleModel->saveAll($data);
            }
            $time = time();
            if (!$user->validity_time || $user->validity_time < $time) {
                $user->validity_time = $time + ($user->meal->days * 86400);
            } else {
                $user->meal_id !== 1 && $user->validity_time = $user->validity_time + ($time + ($user->meal->days * 86400));
            }
            $user->update_vip = 0;
            $user->meal_id = $mealId;
            $user->save();
        }


    }
}